package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.InviteDirectListModel;
import com.banshuibao.bsb_android.model.InviteInDirectListModel;
import com.banshuibao.bsb_android.util.TimeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangpan on 2018/6/12.
 */

public class InviteRecordAdapter extends BaseAdapter {

    private Context context;
    private List<DataHolder> dataList;

    public InviteRecordAdapter(Context context) {
        this.context = context;
        dataList = new ArrayList<>();
    }


    public void setDirectUpData(List<InviteDirectListModel.DataBean> inviteUsers) {
        if (dataList != null) {
            for (int i = 0; i < inviteUsers.size(); i++) {
                DirectData directData = new DirectData();
                directData.setObj(inviteUsers.get(i));
                dataList.add(directData);
            }
            notifyDataSetChanged();
        }
    }

    public void setIndirectUpData(List<InviteInDirectListModel.DataBean> inviteUsers) {
        if (dataList != null) {
            for (int i = 0; i < inviteUsers.size(); i++) {
                InDirectData inDirectData = new InDirectData();
                inDirectData.setObj(inviteUsers.get(i));
                dataList.add(inDirectData);
            }
            notifyDataSetChanged();
        }
    }

    public void setDirectData(List<InviteDirectListModel.DataBean> inviteUsers) {
        if (dataList != null) {
            dataList.clear();
            for (int i = 0; i < inviteUsers.size(); i++) {
                DirectData directData = new DirectData();
                directData.setObj(inviteUsers.get(i));
                dataList.add(directData);
            }
            notifyDataSetChanged();
        }
    }

    public void setIndirectData(List<InviteInDirectListModel.DataBean> inviteUsers) {
        if (dataList != null) {
            dataList.clear();
            for (int i = 0; i < inviteUsers.size(); i++) {
                InDirectData inDirectData = new InDirectData();
                inDirectData.setObj(inviteUsers.get(i));
                dataList.add(inDirectData);
            }
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getType();
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getCount() {
        return dataList != null ? dataList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position).getObj();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        switch (type) {
            case 0:
                return getDirectView(position, convertView, parent);
            case 1:
                return getIndirectView(position, convertView, parent);
        }
        return null;

    }

    public View getIndirectView(int position, View convertView, ViewGroup parent) {
        IndirectViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.invite_indirect_item_layout, null);
            viewHolder = new IndirectViewHolder();
            viewHolder.name = convertView.findViewById(R.id.user_name);
            viewHolder.date = convertView.findViewById(R.id.user_date);
            viewHolder.status = convertView.findViewById(R.id.user_st);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (IndirectViewHolder) convertView.getTag();
        }

        InviteInDirectListModel.DataBean bean = (InviteInDirectListModel.DataBean) dataList.get(position).getObj();
        if (bean != null) {
            if (!TextUtils.isEmpty(bean.getName())) {
                if (dataList != null) {
                    if (position == dataList.size() - 1)
                        viewHolder.name.setBackgroundResource(R.drawable.shape_invite_form_item_left_last);
                    else
                        viewHolder.name.setBackgroundResource(R.drawable.shape_invite_form_item_left);
                } else {
                    if (position == dataList.size() - 1) {
                        viewHolder.name.setBackgroundResource(R.drawable.shape_invite_form_item_left_null_last);
                    } else
                        viewHolder.name.setBackgroundResource(R.drawable.shape_invite_form_item_left_null);
                }
                if (position == dataList.size() - 1) {
                    viewHolder.date.setBackgroundResource(R.drawable.shape_invite_form_item_last);
                    viewHolder.status.setBackgroundResource(R.drawable.shape_invite_form_item_right_last);
                } else {
                    viewHolder.date.setBackgroundResource(R.drawable.shape_invite_form_item);
                    viewHolder.status.setBackgroundResource(R.drawable.shape_invite_form_item_right);
                }
            }
        }
        viewHolder.name.setText(bean.getName());
        viewHolder.date.setText(TimeUtil.format2(bean.getRegDate()));
        String vip = bean.getVip();
        if (!TextUtils.isEmpty(vip)) {
            viewHolder.status.setText(TextUtils.equals(vip, "0") ? "是" : "否");
        } else {
            viewHolder.status.setText("");
        }
        return convertView;
    }

    public View getDirectView(int position, View convertView, ViewGroup parent) {
        DirectViewHolder directViewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.invite_direct_item_layout, null);
            directViewHolder = new DirectViewHolder();
            directViewHolder.name = convertView.findViewById(R.id.user_name);
            directViewHolder.price = convertView.findViewById(R.id.user_price);
            directViewHolder.status = convertView.findViewById(R.id.user_st);
            convertView.setTag(directViewHolder);
        } else {
            directViewHolder = (DirectViewHolder) convertView.getTag();
        }

        InviteDirectListModel.DataBean bean = (InviteDirectListModel.DataBean) dataList.get(position).getObj();
        if (bean != null) {
            if (!TextUtils.isEmpty(bean.getName())) {
                if (dataList != null) {
                    if (position == dataList.size() - 1)
                        directViewHolder.name.setBackgroundResource(R.drawable.shape_invite_form_item_left_last);
                    else
                        directViewHolder.name.setBackgroundResource(R.drawable.shape_invite_form_item_left);
                } else {
                    if (position == dataList.size() - 1) {
                        directViewHolder.name.setBackgroundResource(R.drawable.shape_invite_form_item_left_null_last);
                    } else
                        directViewHolder.name.setBackgroundResource(R.drawable.shape_invite_form_item_left_null);
                }
                if (position == dataList.size() - 1) {
                    directViewHolder.price.setBackgroundResource(R.drawable.shape_invite_form_item_right_last);
                    directViewHolder.status.setBackgroundResource(R.drawable.shape_invite_form_item_last);
                } else {
                    directViewHolder.price.setBackgroundResource(R.drawable.shape_invite_form_item_right);
                    directViewHolder.status.setBackgroundResource(R.drawable.shape_invite_form_item);
                }
            }
        }

        directViewHolder.name.setText(bean.getName());
        if (!TextUtils.isEmpty(bean.getName())) {
            double sumPrice = bean.getRebatePrice();
            Double aDouble = new Double(sumPrice);
            directViewHolder.price.setText(String.valueOf(aDouble.intValue()));
        } else {
            directViewHolder.price.setText("");
        }

        String vip1 = bean.getVip();
        if (!TextUtils.isEmpty(vip1)) {
            directViewHolder.status.setText(TextUtils.equals(vip1, "0") ? "是" : "否");
        } else {
            directViewHolder.status.setText("");
        }
        return convertView;
    }


    public class IndirectViewHolder<InviteInDirectListModel> {
        TextView name, date, status;
    }

    public class DirectViewHolder<InviteDirectListModel> {
        TextView name, price, status;
    }

    public abstract class DataHolder<T> {
        T obj;

        public T getObj() {
            return obj;
        }

        public void setObj(T obj) {
            this.obj = obj;
        }

        public abstract int getType();
    }

    public class DirectData extends DataHolder {

        @Override
        public int getType() {
            return 0;
        }
    }

    public class InDirectData extends DataHolder {

        @Override
        public int getType() {
            return 1;
        }
    }

    public void clear() {
        if (dataList != null) {
            dataList.clear();
        }
    }
}
